<template>
  <div class="announcement-card">
    <div class="header">
      <span class="title">公告</span>
    </div>

    <div v-if="loading" class="loading-state">
      <div class="loader"></div>
    </div>

    <div v-else-if="error" class="error-state">
      {{ error }}
      <button @click="fetchAnnouncements">重试</button>
    </div>

    <div v-else class="announcements-container">
      <!-- 置顶公告 -->
      <div
        v-if="pinnedAnnouncement"
        class="pinned-announcement"
      >
        <div class="announcement-header">
          <div class="badge">
            <PhPushPin :size="14" />
            <span>置顶</span>
          </div>
          <time>{{ formatDate(pinnedAnnouncement.createTime) }}</time>
        </div>
        <div class="announcement-body">
          <h4>{{ pinnedAnnouncement.title }}</h4>
          <p v-html="pinnedAnnouncement.content"></p>
        </div>
      </div>

      <!-- 普通公告列表 -->
      <div class="announcement-list">
        <TransitionGroup name="list">
          <div
            v-for="item in announcements"
            :key="item.id"
            class="announcement-item"
          >
            <div class="item-header">
              <div class="title-wrapper">
                <div class="dot"></div>
                <h5>{{ item.title || '通知' }}</h5>
              </div>
              <time>{{ formatDate(item.createTime) }}</time>
            </div>
            <p class="item-content">{{ item.content }}</p>
          </div>
        </TransitionGroup>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { PhPushPin } from '@phosphor-icons/vue';
import { getAnnouncementList } from '@/api/announcement';
import Message from '@/components/message';

interface Announcement {
  id: string;
  title?: string;
  content: string;
  isPinned: boolean;
  createTime: string;
}

const loading = ref(true);
const error = ref<string | null>(null);
const announcements = ref<Announcement[]>([]);
const pinnedAnnouncement = ref<Announcement | null>(null);

const formatDate = (date: string) => {
  return new Intl.DateTimeFormat('zh-CN', {
    month: 'long',
    day: 'numeric'
  }).format(new Date(date));
};

const fetchAnnouncements = async () => {
  try {
    loading.value = true;
    error.value = null;
    const result = await getAnnouncementList();
    const data = result.data;

    // 分离置顶和普通公告
    pinnedAnnouncement.value = data.find((a: Announcement) => a.isPinned) || null;
    announcements.value = data.filter((a: Announcement) => !a.isPinned);
  } catch (err) {
    error.value = '获取公告失败';
    Message.error('获取公告失败');
    console.error('Failed to load announcements:', err);
  } finally {
    loading.value = false;
  }
};

onMounted(fetchAnnouncements);
</script>

<style scoped lang="less">
.announcement-card {
  background: #fff;
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 24px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);

  .header {
    margin-bottom: 16px;

    .title {
      font-size: 15px;
      font-weight: 600;
      color: #1a1a1a;
      position: relative;
      padding-left: 10px;

      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 3px;
        height: 14px;
        background: #60a5fa;
        border-radius: 2px;
      }
    }
  }

  .pinned-announcement {
    background: linear-gradient(to right, #f0f9ff, #e0f2fe);
    border-radius: 10px;
    padding: 16px;
    margin-bottom: 20px;
    border: 1px solid rgba(96, 165, 250, 0.1);
    transition: all 0.3s ease;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(96, 165, 250, 0.1);
    }

    .announcement-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 12px;

      .badge {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        background: #60a5fa;
        color: #fff;
        padding: 4px 10px;
        border-radius: 6px;
        font-size: 12px;
        font-weight: 500;
        box-shadow: 0 2px 4px rgba(96, 165, 250, 0.2);

        svg {
          opacity: 0.9;
        }
      }

      time {
        font-size: 12px;
        color: #6b7280;
      }
    }

    .announcement-body {
      h4 {
        font-size: 15px;
        font-weight: 600;
        color: #1a1a1a;
        margin: 0 0 8px;
        line-height: 1.4;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 1;
        -webkit-box-orient: vertical;
      }

      p {
        font-size: 13px;
        color: #4b5563;
        line-height: 1.6;
        margin: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
      }
    }
  }

  .announcement-list {
    display: flex;
    flex-direction: column;
    gap: 16px;
    max-height: 300px;
    overflow-y: auto;

    // 隐藏滚动条但保持滚动功能
    -ms-overflow-style: none;  /* IE and Edge */
    scrollbar-width: none;  /* Firefox */

    /* Chrome, Safari and Opera */
    &::-webkit-scrollbar {
      display: none;
    }

    .announcement-item {
      padding: 12px;
      border-radius: 8px;
      background: #f8fafc;
      transition: all 0.3s ease;

      &:hover {
        background: #f1f5f9;
        transform: translateX(4px);
      }

      .item-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 8px;

        .title-wrapper {
          display: flex;
          align-items: center;
          gap: 8px;
          flex: 1;
          min-width: 0;

          .dot {
            width: 6px;
            height: 6px;
            background: #60a5fa;
            border-radius: 50%;
            flex-shrink: 0;
          }

          h5 {
            font-size: 14px;
            font-weight: 500;
            color: #1a1a1a;
            margin: 0;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            flex: 1;
          }
        }

        time {
          font-size: 12px;
          color: #6b7280;
          margin-left: 8px;
        }
      }

      .item-content {
        font-size: 13px;
        color: #4b5563;
        line-height: 1.6;
        margin: 0;
        padding-left: 14px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
    }
  }

  .loading-state {
    text-align: center;
    padding: 20px 0;

    .loader {
      width: 20px;
      height: 20px;
      border: 2px solid #f3f3f3;
      border-top: 2px solid #60a5fa;
      border-radius: 50%;
      margin: 0 auto;
      animation: spin 0.8s linear infinite;
    }
  }

  .error-state {
    text-align: center;
    color: #ff4d4f;
    padding: 16px 0;
    font-size: 13px;

    button {
      margin-left: 8px;
      padding: 4px 12px;
      border: 1px solid currentColor;
      border-radius: 4px;
      background: transparent;
      color: inherit;
      font-size: 12px;
      cursor: pointer;
      transition: all 0.2s ease;

      &:hover {
        background: #ff4d4f;
        color: #fff;
      }
    }
  }
}

// 列表动画
.list-enter-active,
.list-leave-active {
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.list-enter-from {
  opacity: 0;
  transform: translateY(20px);
}

.list-leave-to {
  opacity: 0;
  transform: translateY(-20px);
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

</style>
